package com.ausware.pojo;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;


public class Mysql {
	
	 public static void insertInto(PersonTo p) throws ClassNotFoundException, SQLException  {
	        String URL="jdbc:mysql://localhost:3306/final?useUnicode=true&amp;characterEncoding=utf-8";
	        String USER="root";
	        String PASSWORD="root";
	        //1.加载驱动程序
	        Class.forName("com.mysql.jdbc.Driver");
	        //2.获得数据库链接
	        Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
	        //3.通过数据库的连接操作数据库
	       
	        String sql1 = "select * from xixi where sessionId = ?";
	        String sql2 = "delete from xixi where sessionId = ?";
	        String sql3 = "insert into xixi(sessionId,content,end,startTimeMillis,endTimeMillis) values (?,?,?,?,?)";
	        PreparedStatement st=conn.prepareStatement(sql1);
	        PreparedStatement st2 = conn.prepareStatement(sql2);
	        st.setString(1, p.getSessionId());
	        //先查询数据库中是否已经有相同的sessionId的数据，如果已有，先删除再添加
	        ResultSet rs = st.executeQuery();
	        if(rs.next()){
	        	st2.setString(1, p.getSessionId());
	        	st2.execute();
	        }
	        PreparedStatement st3 = conn.prepareStatement(sql3);
	        st3.setString(1, p.getSessionId());
	        st3.setString(2, p.getContent());
	        st3.setString(3, p.getEnd());
	        st3.setLong(4, p.getStartTimeMillis());
	        st3.setLong(5, p.getEndTimeMillis());
	        //关闭资源
	        st3.execute();
	        rs.close();
	        st.close();
	        conn.close();
	    }
	
}
